<template>
	<view class="search">
		<searchCom @searchClick="searchFun"></searchCom>
		<noData v-if="searchList.length==0"></noData>
		<scroll-view v-else class="list" scroll-y="true" @scrolltolower="getshoplist">
			<shoplistCom :shoplistArr="searchList" ></shoplistCom>
		</scroll-view>
	</view>
</template>

<script>
	import { shoplist } from '../../api/index.js'
	import noData from '../../components/data/nodata.vue'
	import shoplistCom from '../../components/shoplist/shoplistcom.vue'
	import searchCom from '../../components/search/searchCom.vue'
	export default {
		data() {
			return {
				searchList:[],
				searchVal:'',
				status:0,
				page:0,
				limit:20,
				isLoad:false,//判断之前的请求是否还未结束
				isAll:false,//判断是否加载了全部数据
			}
		},
		methods: {
			//点击搜索逻辑
			searchFun:function(data){
				this.searchVal = data.searchComval;
				this.isAll = false;
				this.isLoad = false;
				this.page = 0;
				this.getshoplist(true);
			},
			//获取我的商户列表
			async getshoplist(isSw){
				let that = this;
				if(that.isLoad || that.isAll){ //假如之前的请求未完成或者列表数据已经全部加载完毕，则返回空
					console.log("请求未完成或者列表数据已经全部加载完毕")
					return ;
				}
				try{
					that.isLoad = true;
					let data = await shoplist({
						status:that.status,
						page:that.page,
						limit:that.limit,
						search:that.searchVal
					});
					let dealshoplist = that.dealshoplist(data.data.list);//返回已处理的数据
					if(isSw === true){//判断是否重新加载数据
						that.searchList = dealshoplist;
					}else{
						that.searchList = that.searchList.concat(dealshoplist);
					}
					
					console.log(data)
					that.page += 1;
					that.isLoad = false;
					if(dealshoplist.length < that.limit){
						that.isAll = true;
					}
				}catch(e){
					console.log(e);
					//TODO handle the exception
				}
			},
			//商户数据列表统一处理
			dealshoplist:function(datalist){
				let statusObj = {
					'0':"",
					'1':"审核成功",
					'2':"审核中",
					'3':"审核失败",
					'4':"未认证"
				}
				let deallist = datalist.map((item,index,arr)=>{
					return {
						suid:item.id,//门店id
						shop_name:item.shop_name,//商户名
						uid:item.uid,//用户id
						aid:item.service_code,
						realname:item.service_name,//业务员姓名
						address:item.address,//门店地址
						status:item.shop_status,//审核状态
						statusText:statusObj[item.shop_status],//审核状态文字
						bang:''
					};
				})
				console.log(deallist)
				return deallist;
			}
		},
		components:{
			noData,
			shoplistCom,
			searchCom
		}
	}
</script>

<style lang="scss" scoped>
	.search{
		position: relative;
		height: 100%;
		.searchCom{
			position: fixed;
			width: 95%;
			left: 0;
			right: 0;
			top: 90rpx;
			border-bottom: 1px $uni-border-color-e5 solid;
		}
		.list{
			height: 100%;
			box-sizing: border-box;
			padding-top: 90rpx;
		}
		.nodata{
			padding-top: 200rpx;
		}
	}
</style>
